package com.dming.testrequest;

import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jamlu.framework.model.bean.BaseBean;
import com.jamlu.framework.model.bean.BaseListBean;
import com.jamlu.framework.presenter.impl.BaseRxPresenter;
import com.jamlu.framework.ui.status.StatusSmartRvView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;


/**
 * Created by DMing at 2018/2/23 0023
 */

public class MainPresenter extends BaseRxPresenter<MainView> {


    public MainPresenter(MainView iView) {
        super(iView);
    }

    public void injectStatusNone(){
        getView().requestStatusToast(testRequest(), new Consumer<BaseBean>() {
            @Override
            public void accept(BaseBean baseBean) throws Exception {
                getView().onSuccess(baseBean.getHttpCode());
            }
        });
    }

    public void injectStatusDialog(boolean cancelable){
        getView().requestStatusDialog(cancelable, ApiManager.publicService().toLoginNow(null), new Consumer<BaseBean>() {
            @Override
            public void accept(BaseBean baseBean) throws Exception {
                getView().onSuccess(baseBean.getHttpCode());
            }
        });
    }


    public void injectStatusRequest(ViewGroup viewGroup){
        getView().requestStatusView(viewGroup, testRequest(), new Consumer<BaseBean>() {
            @Override
            public void accept(BaseBean baseBean) throws Exception {
                getView().onSuccess(baseBean.getHttpCode());
            }
        });
    }

    public StatusSmartRvView injectRvStatusRequest(ViewGroup viewGroup, BaseQuickAdapter baseQuickAdapter){
        return getView().requestStatusSmartRv(viewGroup, baseQuickAdapter, testRequestList(), new Consumer<BaseListBean>() {
            @Override
            public void accept(BaseListBean baseListBean) throws Exception {
                getView().onSuccess(baseListBean.getHttpCode());
            }
        });
    }


    public Observable<BaseBean> testRequest(){
        return Observable.create(new ObservableOnSubscribe<BaseBean>() {
            @Override
            public void subscribe(ObservableEmitter<BaseBean> emitter) throws Exception {
                BaseBean baseBean = new BaseBean();
                baseBean.setHttpCode(2000);
                baseBean.setMsg("6666");
                emitter.onNext(baseBean);
            }
        })
                .delay(2, TimeUnit.SECONDS);

    }

    public Observable<BaseListBean> testRequestList(){
        return Observable.create(new ObservableOnSubscribe<BaseListBean>() {
            @Override
            public void subscribe(ObservableEmitter<BaseListBean> emitter) throws Exception {
                List<TestNotify> testNotifies = new ArrayList<>();
                testNotifies.add(new TestNotify("Get Extra 20 % off using your EX Points in Healthy Food from 20 October until 31 October 2017.","20 Oct 2017",1));
                testNotifies.add(new TestNotify("Get Extra 20 % off using your EX Points in Healthy Food from 20 October until 31 October 2017.","20 Oct 2017",1));
                testNotifies.add(new TestNotify("Get Extra 20 % off using your EX Points in Healthy Food from 20 October until 31 October 2017.","20 Oct 2017",1));
                TestListBean baseBean = new TestListBean(testNotifies);
                baseBean.setHttpCode(2000);
                baseBean.setMsg("6666");
                emitter.onNext(baseBean);
            }
        })
        .delay(2, TimeUnit.SECONDS);

    }
}
